package ext.acae2.change.autonumber;

import java.io.Serializable;
import java.util.ArrayList;

import wt.change2.WTChangeOrder2;
import wt.events.KeyedEvent;
import wt.events.KeyedEventListener;
import wt.fc.PersistenceManagerEvent;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.services.ManagerException;
import wt.services.ServiceEventListenerAdapter;
import wt.services.StandardManager;
import wt.util.WTException;
import ext.acae2.change.helper.ECNHelper;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-5-10 下午4:36:36
 */
public class ECNStandardListenerService extends StandardManager implements
		ECNServer, Serializable {

	/**
	 * @description:TODO
	 */
	private static final long serialVersionUID = 1L;

	private static final String CLASSNAME = ECNStandardListenerService.class
			.getName();
	private KeyedEventListener listener;

	public String getConceptualClassname() {
		return CLASSNAME;
	}

	public static ECNStandardListenerService newECNStandardListenerService()
			throws WTException {
		ECNStandardListenerService instance = new ECNStandardListenerService();
		instance.initialize();
		return instance;
	}

	class AsesEventListener extends ServiceEventListenerAdapter {

		public AsesEventListener(String manager_name) {
			super(manager_name);
		}
		/**
		 * Listening for event - POST_STORE POST_MODIFY
		 * 
		 * @seenotifyVetoableEvent
		 * @param Object
		 *            event The event for which service to be created
		 * @throws Exception
		 **/
		public void notifyVetoableEvent(Object event) throws Exception {
			if (!(event instanceof KeyedEvent)) {
				return;
			}

			KeyedEvent keyedEvent = (KeyedEvent) event;
			Object target = keyedEvent.getEventTarget();
			if (keyedEvent.getEventType().equals(
					PersistenceManagerEvent.POST_STORE)
					|| keyedEvent.getEventType().equals(
							PersistenceManagerEvent.POST_MODIFY)) {
			}
		}

	}
	protected void performStartupProcess() throws ManagerException {
		listener = new AsesEventListener(this.getConceptualClassname());
		getManagerService().addEventListener(listener,PersistenceManagerEvent.generateEventKey(PersistenceManagerEvent.POST_STORE));
		getManagerService().addEventListener(listener,PersistenceManagerEvent.generateEventKey(PersistenceManagerEvent.POST_MODIFY));
	}
	
	private void changeECNNumber(Object target){
		if(target instanceof WTChangeOrder2){
			WTChangeOrder2 ecn = (WTChangeOrder2)target;
			ArrayList<WTObject> affectObj = ECNHelper.getChangeAffectItem(ecn);
			for(WTObject obj:affectObj){
				if(obj instanceof WTPart){
					WTPart part = (WTPart)obj;
				}
			}
		}
		
	}

}
